﻿using Tutorial1_Spawner;
using Unity.Collections;
using Unity.Entities;
using Unity.Transforms;

namespace Tutorial4_Singleton
{
    [UpdateInGroup(typeof(Tutorial4SystemGroup))]
    public partial struct CubeRotateAndMoveSystem : ISystem
    {
        public void OnCreate(ref SystemState state)
        {
            state.RequireForUpdate<RandomSingleton>();
        }

        public void OnUpdate(ref SystemState state)
        {
            // 虽然是单例，但是没有达到每个Cube都使用不同随机数的目的
            var random = SystemAPI.GetSingleton<RandomSingleton>();

            foreach (var rotateAndMoveAspect in SystemAPI.Query<Tutorial4_Singleton_Aspect.CubeRotateAndMoveAspect>())
            {
                rotateAndMoveAspect.RotateAndMove(ref random.RandomInstance, SystemAPI.Time.ElapsedTime, SystemAPI.Time.DeltaTime);
            }
        }
    }
}